<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Version;
use OSS\OssClient;

class VersionController extends Controller
{
	public function __construct()
	{
		view()->share('menu', 'version');
	}
	//版本列表
    public function index()
    {
    	$data = Version::orderBy('created_at', 'desc')->paginate(10);

    	return view('admin/version/index', compact('data'));
    }
    //新增版本
    public function create()
    {
    	return view('admin/version/create');
    }
    //保存
    public function store(Request $request)
    {
    	$version = $request->get('version');
    	$is_force = $request->get('is_force');
        $note = $request->get('note');
        $type = $request->get('type');

    	$oss = new OssClient(config('alioss.accessKeyId'), config('alioss.accessKeySecret'), config('alioss.endpoint'));

    	if ($request->has('path')) {
            $path = $request->file('path');
            if ($path) {
                if ($path->isValid()) {
                    $realPath = $path->getRealPath(); //临时文件的绝对路径
                    $ext      = $path->getClientOriginalExtension(); // 扩展名
                    $filename = date('YmdHis') . '_' . uniqid() . '.' . $ext; //文件名
                    $osspath  = 'block_chain/' . $filename; //oss路径
                    $oss->uploadFile(config('alioss.bucket'), $osspath, $realPath); //上传文件到阿里云

                    $path = $osspath; //数据库保存半路径文件名
                }
            }
        }

    	Version::create([ 
    		'version' => $version,
    		'is_force' => $is_force,
    		'oss_path' => $path,
            'note' => $note,
            'type' => $type,
    	]);

    	return redirect('admin/version');
    }

    public function show($id)
    {
        $data = Version::find($id);

        return view('admin/version/show', compact('data'));
    }

    //编辑版本
    public function edit($id)	
    {
    	$data = Version::find($id);

    	return view('admin/version/edit', compact('data'));
    }
    //更新
    public function update(Request $request, $id)
    {
    	$data = Version::find($id);

    	$version = $request->get('version');
    	$is_force = $request->get('is_force');
        $note = $request->get('note');
        $type = $request->get('type');

    	$data->update([ 
    		'version' => $version,
    		'is_force' => $is_force,
            'note' => $note,
            'type' => $type,
    	]);


    	$oss = new OssClient(config('alioss.accessKeyId'), config('alioss.accessKeySecret'), config('alioss.endpoint'));

    	if ($request->has('path')) {
            $path = $request->file('path');
            if ($path) {
                if ($path->isValid()) {
                    $realPath = $path->getRealPath(); //临时文件的绝对路径
                    $ext      = $path->getClientOriginalExtension(); // 扩展名
                    $filename = date('YmdHis') . '_' . uniqid() . '.' . $ext; //文件名
                    $osspath  = 'block_chain/' . $filename; //oss路径
                    $oss->uploadFile(config('alioss.bucket'), $osspath, $realPath); //上传文件到阿里云

                    $path = '/' . $osspath; //数据库保存半路径文件名

                    $data->update([ 'oss_path' => $path ] );
                }
            }
        }

    	return redirect('admin/version');
    }
}
